package metier;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

import dao.DepartementDAO;


public class Departement {
	private int id;
	private String nom;
	private boolean _builtFromDB;
	private DepartementDAO dao;
	private Collection<Enseignant> profs = null;

	public Departement() {
		profs = new ArrayList<Enseignant>();
		_builtFromDB=false;
	}

	public Departement(String nom) {
		this.nom=nom; id=-1; _builtFromDB=false;
	}

	public String toString() {
		return "Département " + id + " : " + nom ;
	}

	public int getId() {return id;}

	public void setId(int id) {this.id=id;} 

	public String getNom() {return nom;}

	public void setNom(String nom) {this.nom=nom;}

	public boolean is_builtFromDB() {
		return _builtFromDB;
	}

	public void set_builtFromDB(boolean fromDB) {
		_builtFromDB = fromDB;
	}

	public boolean equals(Object other) { 
		if (this == other) return true; 
		if ( !(other instanceof Departement) ) return false;  
		final Departement obj = (Departement) other; 
		if ( obj.getId()!=getId() ) 
			return false;           
		return true; 
	}

	public int hashCode(){
		return id * 39 ;
	}
	
	public void save() throws IOException, ClassNotFoundException, SQLException {
		dao = DepartementDAO.getDAO();
		dao.update(this);
	}
	
	public void delete() throws IOException, ClassNotFoundException, SQLException, Exception {
		dao = DepartementDAO.getDAO();
		dao.deleteByID(this.id);
	}
	
	public void addProf(Enseignant prof){
		profs.add(prof);
	}
	
	public void removeProf(Enseignant prof){
		profs.remove(prof);
	}
	
	public void updateProf(Enseignant prof){
		removeProf(prof);
		addProf(prof);
	}
	
	public static ArrayList<Departement> load() throws IOException, ClassNotFoundException, SQLException{
		DepartementDAO dptDAO = DepartementDAO.getDAO();
		return dptDAO.getDepartement();
	}

	public Collection<Enseignant> getProfs() {
		return profs;
	}
	
}
